package com.tao.plan.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.common.exception.EmailBandingException;
import com.tao.plan.common.utils.CaptchaGenerator;
import com.tao.plan.common.utils.EmailUtil;
import com.tao.plan.common.utils.RedisUtil;
import com.tao.plan.domain.dto.EmailBandingDto;
import com.tao.plan.domain.dto.LoginDto;
import com.tao.plan.domain.pojo.PUser;
import com.tao.plan.service.PUserService;
import com.tao.plan.common.utils.JwtUtil;
import com.tao.plan.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {
	@Autowired
	private PUserService pUserService;
	@Autowired
	private JwtUtil jwtUtil;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private EmailUtil emailUtil;
	@PostMapping("/login")
	public R doLogin(@RequestBody LoginDto loginDto){
		PUser user = pUserService.wxLogin(loginDto);
		PUser one = pUserService.getOne(new LambdaQueryWrapper<PUser>().eq(PUser::getOpenid, user.getOpenid()));
		// token生成
		Map<String,String> tokenInfo = new HashMap<>();
		if(one == null){
			pUserService.save(user);
			tokenInfo.put("uid",user.getId().toString());
		}else{
			tokenInfo.put("uid",one.getId().toString());
		}
		String token = jwtUtil.createToken(tokenInfo);
		// 打印结果
		return R.build().success().put("data",token);
	}

	/**
	 * 获取邮箱验证码
	 */
	@GetMapping("/email/vertifyCode")
	public R vertifyCode(@RequestParam("email") String email){
		// 生成验证码
		String code = CaptchaGenerator.generateCaptcha(6);
		// 将code发送到邮箱里边
		String content = emailUtil.readCodeHtmlTemplate("/template/emailVertifyCode.html", code);
		emailUtil.sendHtmlMail(email,"我要当planner 验证码",content);
		redisUtil.setVerifyCode(email,code);
		return R.build().success().put("data",code);
	}

	/**
	 * 邮箱绑定
	 */
	@PostMapping("/email/binding")
	public R binding(@RequestBody EmailBandingDto emailBandingDto){
		String email = emailBandingDto.getEmail();
		String code = emailBandingDto.getCode();
		String verifyCode = redisUtil.getVerifyCode(email);
		if (!code.equals(verifyCode)){
			// 验证码不一致
			throw new EmailBandingException("验证码不一致或验证码已过期");
		}
		// 验证码一样了, 保存
		PUser pUser = new PUser();
		pUser.setId(Long.valueOf(BaseThreadLocal.getUid()));
		pUser.setEmail(email);
		pUserService.updateById(pUser);
		return R.build().success();
	}

	/**
	 * 用户信息获取
	 */
	@GetMapping("/info")
	public R userInfo(){
		PUser byId = pUserService.getById(BaseThreadLocal.getUid());
		return R.build().success().put("data",byId);
	}

	/**
	 * 推送设置
	 */
	@PostMapping("/isPush/{isPush}")
	public R setIsPush(@PathVariable("isPush") Integer isPush){
		PUser pUser = new PUser();
		pUser.setId(Long.valueOf(BaseThreadLocal.getUid()));
		pUser.setIsPush(isPush);
		pUserService.updateById(pUser);
		return R.build().success();
	}
}
